import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { patientTransCreate } from "@/api/components/transferPatient";
import { useTranslation } from "react-i18next";

interface PropsType {
	register: Fn;
	success?: Fn;
}

const TransferPatient = (props: PropsType) => {
	const { t } = useTranslation();

	const { register, success } = props;

	const getTitle = "Add Transfer Patient Record";
	const formSchemas: FormSchema[] = [
		{
			label: t("patientBasic.patient_id"),
			field: "patientId",
			component: "Input",
			componentProps: {
				disabled: true
			}
		},
		{
			label: t("patientBasic.patient_code"),
			field: "patCardno",
			component: "Input",
			componentProps: {
				disabled: true
			}
		},
		{
			label: t("viewComponents.transferPatient.transferType"),
			field: "transType",
			component: "Select",
			componentProps: {
				options: [
					{
						label: "Inside Hospital",
						value: 1
					},
					{
						label: "Outside Hospital",
						value: 2
					}
				]
			}
		},
		{
			label: t("viewComponents.transferPatient.transferFrom"),
			field: "transFrom",
			component: "Select",
			componentProps: {
				options: [
					{
						label: "Test Clinic 1",
						value: "test_clinic_1"
					},
					{
						label: "Test Clinic 2",
						value: "test_clinic_2"
					}
				]
			},
			ifShow: ({ values }: Recordable) => values.transType === 1
		},
		{
			label: t("viewComponents.transferPatient.transferTo"),
			field: "transTo",
			component: "Select",
			componentProps: {
				options: [
					{
						label: "Test Clinic 1",
						value: "test_clinic_1"
					},
					{
						label: "Test Clinic 2",
						value: "test_clinic_2"
					}
				]
			},
			ifShow: ({ values }: Recordable) => values.transType === 1
		},
		{
			label: t("viewComponents.transferPatient.transferTime"),
			field: "referralDtime",
			component: "DatePicker",
			componentProps: {
				showTime: true
			},
			ifShow: ({ values }: Recordable) => values.transType === 1
		},
		{
			label: t("viewComponents.transferPatient.transferFrom"),
			field: "clinicFrom",
			component: "Select",
			componentProps: {
				options: [
					{
						label: "Test Hospital 1",
						value: "test_his_1"
					},
					{
						label: "Test Hospital 2",
						value: "test_his_2"
					}
				]
			},
			ifShow: ({ values }: Recordable) => values.transType === 2
		},
		{
			label: t("viewComponents.transferPatient.transferTo"),
			field: "clinicTo",
			component: "Select",
			componentProps: {
				options: [
					{
						label: "Test Hospital 1",
						value: "test_his_1"
					},
					{
						label: "Test Hospital 2",
						value: "test_his_2"
					}
				]
			},
			ifShow: ({ values }: Recordable) => values.transType === 2
		},
		{
			label: t("viewComponents.transferPatient.transferTime"),
			field: "transDtime",
			component: "DatePicker",
			componentProps: {
				showTime: true
			},
			ifShow: ({ values }: Recordable) => values.transType === 2
		},
		{
			label: t("viewComponents.transferPatient.referralPaper"),
			field: "referralPaper",
			component: "InputTextArea"
		},
		{
			label: t("viewComponents.transferPatient.referralDoctor"),
			field: "referralDoctor",
			component: "Select",
			componentProps: {
				options: [
					{
						label: "Test Doctor 1",
						value: "test_doctor_1"
					},
					{
						label: "Test Doctor 1",
						value: "test_doctor_2"
					}
				]
			}
		},
		{
			label: t("viewComponents.transferPatient.trans2nd"),
			field: "trans2nd",
			component: "Checkbox"
		}
	];
	const [registerForm, { resetFields, setFieldsValue, validateFields }] = useForm({
		labelWidth: 120,
		baseColProps: { span: 24 },
		schemas: formSchemas,
		initialValues: {
			transType: 2
		}
	});

	const [registerDrawer, { closeDrawer, setDrawerProps }] = useDrawerInner(register, data => {
		resetFields();
		setDrawerProps({ confirmLoading: true });
		setFieldsValue({
			...data.record
		});
		setDrawerProps({ confirmLoading: false });
	});

	const handlerOk = async () => {
		try {
			setDrawerProps({ confirmLoading: true });
			const values = await validateFields();
			const res = await patientTransCreate(values);
			closeDrawer();
			success && success(res);
		} finally {
			setDrawerProps({ confirmLoading: false });
		}
	};

	return (
		<BasicDrawer title={getTitle} register={registerDrawer} showFooter={true} ok={handlerOk}>
			<BasicForm register={registerForm}></BasicForm>
		</BasicDrawer>
	);
};

export default TransferPatient;
